﻿package com.petermacko.effects.pixelwarp {
	import flash.events.*
	import flash.display.*;
	import flash.filters.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import flash.geom.Matrix;
	import flash.utils.Timer;

	public class digitalWarpOut extends MovieClip
	{
		private var randomRotationalnumX:Number;
		private var randomRotationalnumY:Number;
		private var randomDriftX:Number;
		private var randomDriftY:Number;
		private var randomDriftZ:uint;
		private var targetItem:MovieClip;
		private var targetDaddy:MovieClip;
		private var targetMommy:MovieClip;
		private var removalArray:Array;
		private var myBitmapSnapshot:BitmapData;
		private var mySnapshotBitmap:Bitmap;
		private var totalSectionsWidth:Number;
		private var totalSectionsHeight:Number;
		private var bitmapMatrix:Matrix;
		private var mySnapPicture:BitmapData;
		private var mySnapShot:Bitmap;
		private var tempSprite:MovieClip;
		private var myGlowFilter:GlowFilter;
		private var myBlur:BlurFilter;
		private var tempRemoval:MovieClip;
		
		public function warpOut(targetItem:MovieClip):void
			{
				
				myGlowFilter = new GlowFilter(0xffffff,1,5,5,1,1,true,false);
				
				myBlur = new BlurFilter(0,0,1);
				
				randomRotationalnumX = 0;
				randomRotationalnumY = 0;
				randomDriftX = 0;
				randomDriftY = 0;
				randomDriftZ = 0;
				
				//targetItem = new MovieClip();***************
				//targetItem = *********************************************8
				targetDaddy = targetItem.parent as MovieClip;
				targetMommy = new MovieClip();
				targetDaddy.addChild(targetMommy);
				targetMommy.x = targetItem.x;
				targetMommy.y = targetItem.y;
				
				//var removalArray:Array = new Array();
				
				//Initial Snapshot Setup
				myBitmapSnapshot = new BitmapData(targetItem.width, targetItem.height, true, 0xFFFFFF);
				mySnapshotBitmap = new Bitmap(myBitmapSnapshot);
				//End Initial Snapshot Setup
				
				//Calculate numbers for sectioning
				totalSectionsWidth = targetItem.width / 10;
				totalSectionsHeight = targetItem.height / 7;
				//End Sections Calc
				
				
				myBitmapSnapshot.draw(targetItem)
				clearTheStage();//Call the delete function
				
				function clearTheStage():void
				{	
				
				sliceAndDraw();
				//var fnxTw:Tween = new Tween(targetItem, "alpha", Strong.easeOut, 1, 0, .5, true);
				//fnxTw.addEventListener(TweenEvent.MOTION_FINISH, removeOriginalClip);
				targetDaddy.removeChild(targetItem);
				
				}
				
				
				
				function moveSprites(e:Event):void//Move the slices away in a fade
				{
					myGlowFilter.blurX += Math.random() * .02;
					myGlowFilter.blurY += Math.random() * .02;
					myGlowFilter.strength = 2;
					e.target.alpha -= Math.random() * .2;
					
					if(e.target.alpha <= 0)
					{
						tempRemoval = new MovieClip();
						tempRemoval = e.target as MovieClip;
						if(tempRemoval.parent)
						{
							tempRemoval.parent.removeChild(tempRemoval);
							e.target.removeEventListener(Event.ENTER_FRAME, moveSprites);
						}
					}else{
						e.target.filters = [myGlowFilter];
					}
				}
				
				function sliceAndDraw():void
				{
					
					mySnapshotBitmap.bitmapData = myBitmapSnapshot;
					mySnapshotBitmap.addEventListener(Event.ENTER_FRAME, GETRIDOFIT);
					mySnapshotBitmap.x = targetItem.x;
					mySnapshotBitmap.y = targetItem.y;
					targetDaddy.addChild(mySnapshotBitmap);//Add the master bitmap to draw from
					targetDaddy.swapChildren(targetMommy, mySnapshotBitmap);
					//var fnTw:Tween = new Tween(mySnapshotBitmap, "alpha", Strong.easeOut, 1, 0, .5, true);
					//fnTw.addEventListener(TweenEvent.MOTION_FINISH, GETRIDOFIT);
				//Begin section drawing
				for( var j:uint = 0; j < 100; j++)
				{
				bitmapMatrix = new Matrix(1,0,0,1,0,0);//scaling/skewing/skewing/scaling/xpos/ypos
				mySnapPicture = new BitmapData((Math.random() * totalSectionsWidth + 10), (Math.random() * totalSectionsHeight + 10), true, 0xFFFFFF);
				mySnapShot = new Bitmap(mySnapPicture);
				tempSprite = new MovieClip();
					
				if(j < 10){	
					bitmapMatrix.tx -= (j * totalSectionsWidth);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = j * totalSectionsWidth;
					//tempSprite.y = totalSectionsHeight * j;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					targetMommy.addChild(tempSprite);
				}
				
				
				if(j >= 11 && j < 22)
				{
					bitmapMatrix.tx -= ((j - 11) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 11) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				if(j >= 22 && j < 33)
				{
					bitmapMatrix.tx -= ((j - 22) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 2);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 22) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 2;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
				
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 33 && j < 44)
				{
					bitmapMatrix.tx -= ((j - 33) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 3);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 33) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 3;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 44 && j < 55)
				{
					bitmapMatrix.tx -= ((j - 44) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 4);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 44) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 4;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 55 && j < 66)
				{
					bitmapMatrix.tx -= ((j - 55) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 5);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 55) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 5;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 66 && j < 77)
				{
					bitmapMatrix.tx -= ((j - 66) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 6);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 66) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 6;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 77 && j < 88)
				{
					bitmapMatrix.tx -= ((j - 77) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 7);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 77) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 7;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
				
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.addChild(tempSprite);
				}
				
				if(j >= 88 && j < 99)
				{
					bitmapMatrix.tx -= ((j - 88) * totalSectionsWidth);
					bitmapMatrix.ty = -(totalSectionsHeight * 8);
					
					mySnapPicture.draw(targetItem,bitmapMatrix,null,null,null,false);
					tempSprite.x = ((j - 88) * totalSectionsWidth);
					tempSprite.y = totalSectionsHeight * 8;
					tempSprite.addEventListener(Event.ENTER_FRAME, moveSprites);
					mySnapShot.bitmapData = mySnapPicture;
					tempSprite.addChild(mySnapShot);
					
					tempSprite.randomRotationalnumX = Math.random() * 1 - 1;
					tempSprite.randomRotationalnumY = Math.random() * 1 - 1;
					tempSprite.randomDriftX = Math.random() * 1 - 1;
					tempSprite.randomDriftY = Math.random() * 1 - 1;
					tempSprite.randomDriftZ = Math.random() * 1 - 1;
					
					targetMommy.x = targetItem.x;
					targetMommy.y = targetItem.y;
					
					targetMommy.addChild(tempSprite);
				}
				
				}
				
					
				}

			function GETRIDOFIT(e:Event):void {
				mySnapshotBitmap.alpha -= .2;
				if(mySnapshotBitmap.alpha <= 0 && mySnapshotBitmap.parent != null) {
					targetDaddy.removeChild(mySnapshotBitmap);
				}
			}
		}
	}
}